//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_sprit_api_serv

import (
	"context"
	"time"

	"atomgit.com/openlinksaas-org/api-server/api_common"
	"atomgit.com/openlinksaas-org/api-server/dao"
	"atomgit.com/openlinksaas-org/api-server/dao/issue_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_entry_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/user_dao"
	"atomgit.com/openlinksaas-org/api-server/utils"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/notices/notices_project"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_api"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_entry_api"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_sprit_api"
	"github.com/google/uuid"
	"go.mongodb.org/mongo-driver/mongo"
)

func (apiImpl *ProjectSpritApiImpl) SetSummaryState(ctx context.Context, req *project_sprit_api.SetSummaryStateRequest) (*project_sprit_api.SetSummaryStateResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &project_sprit_api.SetSummaryStateResponse{
			Code:   project_sprit_api.SetSummaryStateResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	projectItem, _, roleItem, err := utils.GetProjectMemberRole(ctx, req.ProjectId, sessionItem.UserId)
	if err == utils.ErrNoProject {
		return &project_sprit_api.SetSummaryStateResponse{
			Code:   project_sprit_api.SetSummaryStateResponse_CODE_NO_PROJECT,
			ErrMsg: "项目不存在",
		}, nil
	} else if err == utils.ErrNoRole {
		return &project_sprit_api.SetSummaryStateResponse{
			Code:   project_sprit_api.SetSummaryStateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	} else if err != nil {
		return nil, err
	}
	if projectItem.Closed {
		return &project_sprit_api.SetSummaryStateResponse{
			Code:   project_sprit_api.SetSummaryStateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	spritItem, err := issue_dao.SpritDao.Get(ctx, req.ProjectId, req.SpritId)
	if err != nil {
		return &project_sprit_api.SetSummaryStateResponse{
			Code:   project_sprit_api.SetSummaryStateResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}
	if !(projectItem.OwnerUserId == sessionItem.UserId || roleItem.BasicInfo.Admin) {
		return &project_sprit_api.SetSummaryStateResponse{
			Code:   project_sprit_api.SetSummaryStateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	if spritItem.SummaryState == uint32(req.SummaryState) {
		return &project_sprit_api.SetSummaryStateResponse{
			Code: project_sprit_api.SetSummaryStateResponse_CODE_OK,
		}, nil
	}

	nowTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = issue_dao.SpritDao.UpdateSummaryState(sessCtx, req.ProjectId, req.SpritId, uint32(req.SummaryState))
		if err != nil {
			return nil, err
		}
		err = project_entry_dao.EntryDao.SetUpdateInfo(sessCtx, req.SpritId, req.ProjectId, sessionItem.UserId, nowTime)
		if err != nil {
			return nil, err
		}
		return &project_sprit_api.SetSummaryStateResponse{
			Code: project_sprit_api.SetSummaryStateResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	go api_common.SendNoticeToProjectScope(context.Background(), req.ProjectId, &notices_project.UpdateSpritNotice{
		ProjectId: req.ProjectId,
		SpritId:   req.SpritId,
	})
	return ret.(*project_sprit_api.SetSummaryStateResponse), nil
}

func (apiImpl *ProjectSpritApiImpl) AddSummaryItem(ctx context.Context, req *project_sprit_api.AddSummaryItemRequest) (*project_sprit_api.AddSummaryItemResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &project_sprit_api.AddSummaryItemResponse{
			Code:   project_sprit_api.AddSummaryItemResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	projectItem, _, roleItem, err := utils.GetProjectMemberRole(ctx, req.ProjectId, sessionItem.UserId)
	if err == utils.ErrNoProject {
		return &project_sprit_api.AddSummaryItemResponse{
			Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_PROJECT,
			ErrMsg: "项目不存在",
		}, nil
	} else if err == utils.ErrNoRole {
		return &project_sprit_api.AddSummaryItemResponse{
			Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	} else if err != nil {
		return nil, err
	}
	if projectItem.Closed {
		return &project_sprit_api.AddSummaryItemResponse{
			Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	entryItem, err := project_entry_dao.EntryDao.Get(ctx, req.SpritId, req.ProjectId)
	if err != nil {
		return &project_sprit_api.AddSummaryItemResponse{
			Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}
	if entryItem.EntryType != uint32(project_entry_api.ENTRY_TYPE_ENTRY_TYPE_SPRIT) {
		return &project_sprit_api.AddSummaryItemResponse{
			Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}

	hasUpdatePerm := api_common.CheckEntryCanUpdate(sessionItem, projectItem, roleItem, entryItem)

	if !hasUpdatePerm {
		return &project_sprit_api.AddSummaryItemResponse{
			Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	spritItem, err := issue_dao.SpritDao.Get(ctx, req.ProjectId, req.SpritId)
	if err != nil {
		return &project_sprit_api.AddSummaryItemResponse{
			Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}
	if spritItem.SummaryState != uint32(project_sprit_api.SUMMARY_STATE_SUMMARY_COLLECT) {
		return &project_sprit_api.AddSummaryItemResponse{
			Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "不是总结收集阶段",
		}, nil
	}

	//检查标签
	if req.TagId != "" {
		tagItem, err := project_dao.TagInfoDao.Get(ctx, req.ProjectId, req.TagId)
		if err != nil {
			return &project_sprit_api.AddSummaryItemResponse{
				Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_TAG,
				ErrMsg: "标签不存在",
			}, nil
		}
		if !tagItem.UseInSpritSummary {
			return &project_sprit_api.AddSummaryItemResponse{
				Code:   project_sprit_api.AddSummaryItemResponse_CODE_NO_TAG,
				ErrMsg: "标签不存在",
			}, nil
		}
	}

	summaryItemId := uuid.NewString()
	nowTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = issue_dao.SummaryItemDao.Insert(sessCtx, &issue_dao.SummaryItemInfoDbItem{
			SummaryItemId: summaryItemId,
			SpritId:       req.SpritId,
			Content:       req.Content,
			CreateUserId:  sessionItem.UserId,
			CreateTime:    nowTime,
			GroupId:       summaryItemId,
			TagId:         req.TagId,
		})
		if err != nil {
			return nil, err
		}
		return &project_sprit_api.AddSummaryItemResponse{
			Code:          project_sprit_api.AddSummaryItemResponse_CODE_OK,
			SummaryItemId: summaryItemId,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*project_sprit_api.AddSummaryItemResponse), nil
}

func (apiImpl *ProjectSpritApiImpl) UpdateSummaryItem(ctx context.Context, req *project_sprit_api.UpdateSummaryItemRequest) (*project_sprit_api.UpdateSummaryItemResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	projectItem, _, roleItem, err := utils.GetProjectMemberRole(ctx, req.ProjectId, sessionItem.UserId)
	if err == utils.ErrNoProject {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_PROJECT,
			ErrMsg: "项目不存在",
		}, nil
	} else if err == utils.ErrNoRole {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	} else if err != nil {
		return nil, err
	}
	if projectItem.Closed {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	entryItem, err := project_entry_dao.EntryDao.Get(ctx, req.SpritId, req.ProjectId)
	if err != nil {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}
	if entryItem.EntryType != uint32(project_entry_api.ENTRY_TYPE_ENTRY_TYPE_SPRIT) {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}

	hasUpdatePerm := api_common.CheckEntryCanUpdate(sessionItem, projectItem, roleItem, entryItem)

	if !hasUpdatePerm {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	spritItem, err := issue_dao.SpritDao.Get(ctx, req.ProjectId, req.SpritId)
	if err != nil {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}
	if spritItem.SummaryState != uint32(project_sprit_api.SUMMARY_STATE_SUMMARY_COLLECT) {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "不是总结收集阶段",
		}, nil
	}
	summaryItem, err := issue_dao.SummaryItemDao.Get(ctx, req.SpritId, req.SummaryItemId)
	if err != nil {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_SUMMARY_ITEM,
			ErrMsg: "总结条目不存在",
		}, nil
	}
	if summaryItem.CreateUserId != sessionItem.UserId {
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	//检查标签
	if req.TagId != "" {
		tagItem, err := project_dao.TagInfoDao.Get(ctx, req.ProjectId, req.TagId)
		if err != nil {
			return &project_sprit_api.UpdateSummaryItemResponse{
				Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_TAG,
				ErrMsg: "标签不存在",
			}, nil
		}
		if !tagItem.UseInSpritSummary {
			return &project_sprit_api.UpdateSummaryItemResponse{
				Code:   project_sprit_api.UpdateSummaryItemResponse_CODE_NO_TAG,
				ErrMsg: "标签不存在",
			}, nil
		}
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = issue_dao.SummaryItemDao.UpdateTagAndContent(sessCtx, req.SpritId, req.SummaryItemId, req.TagId, req.Content)
		if err != nil {
			return nil, err
		}
		return &project_sprit_api.UpdateSummaryItemResponse{
			Code: project_sprit_api.UpdateSummaryItemResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}

	return ret.(*project_sprit_api.UpdateSummaryItemResponse), nil
}

func (apiImpl *ProjectSpritApiImpl) ListSummaryItem(ctx context.Context, req *project_sprit_api.ListSummaryItemRequest) (*project_sprit_api.ListSummaryItemResponse, error) {
	emptyList := []*project_sprit_api.SummaryItemInfo{}

	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &project_sprit_api.ListSummaryItemResponse{
			Code:     project_sprit_api.ListSummaryItemResponse_CODE_WRONG_SESSION,
			ErrMsg:   "会话不存在",
			ItemList: emptyList,
		}, nil
	}
	_, _, _, err := utils.GetProjectMemberRole(ctx, req.ProjectId, sessionItem.UserId)
	if err == utils.ErrNoProject {
		return &project_sprit_api.ListSummaryItemResponse{
			Code:     project_sprit_api.ListSummaryItemResponse_CODE_NO_PROJECT,
			ErrMsg:   "项目不存在",
			ItemList: emptyList,
		}, nil
	} else if err == utils.ErrNoRole {
		return &project_sprit_api.ListSummaryItemResponse{
			Code:     project_sprit_api.ListSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg:   "没有权限",
			ItemList: emptyList,
		}, nil
	} else if err != nil {
		return nil, err
	}
	spritItem, err := issue_dao.SpritDao.Get(ctx, req.ProjectId, req.SpritId)
	if err != nil {
		return &project_sprit_api.ListSummaryItemResponse{
			Code:     project_sprit_api.ListSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg:   "迭代不存在",
			ItemList: emptyList,
		}, nil
	}
	itemList, err := issue_dao.SummaryItemDao.List(ctx, req.SpritId, spritItem.SummaryState == uint32(project_sprit_api.SUMMARY_STATE_SUMMARY_COLLECT), sessionItem.UserId,
		req.FilterByTagId, req.TagId)
	if err != nil {
		return nil, err
	}
	//补全成员信息
	userIdList := []string{}
	for _, item := range itemList {
		userIdList = append(userIdList, item.CreateUserId)
	}
	userItemList, err := user_dao.UserInfoDao.ListById(ctx, userIdList)
	if err != nil {
		return nil, err
	}
	userItemMap := map[string]*user_dao.UserInfoDbItem{}
	for _, userItem := range userItemList {
		userItemMap[userItem.UserId] = userItem
	}
	//获取标签信息
	tagItemList, err := project_dao.TagInfoDao.List(ctx, req.ProjectId, project_api.TAG_SCOPE_TYPE_TAG_SCOPE_SPRIT_SUMMARY)
	if err != nil {
		return nil, err
	}
	tagItemMap := map[string]*project_dao.TagInfoDbItem{}
	for _, tagItem := range tagItemList {
		tagItemMap[tagItem.TagId] = tagItem
	}
	//生成结果
	infoList := []*project_sprit_api.SummaryItemInfo{}
	for _, item := range itemList {
		info := item.ToSummaryItem()
		userItem, ok := userItemMap[item.CreateUserId]
		if ok {
			info.CreateDisplayName = userItem.BasicInfo.DisplayName
			info.CreateLogoUri = userItem.BasicInfo.LogoUri
		}
		//设置标签
		tagItem, ok := tagItemMap[item.TagId]
		if ok {
			info.TagInfo = &project_sprit_api.SummaryTag{
				TagId:   tagItem.TagId,
				TagName: tagItem.TagName,
				BgColor: tagItem.BgColor,
			}
		} else {
			info.TagInfo = &project_sprit_api.SummaryTag{}
		}
		infoList = append(infoList, info)
	}
	return &project_sprit_api.ListSummaryItemResponse{
		Code:     project_sprit_api.ListSummaryItemResponse_CODE_OK,
		ItemList: infoList,
	}, nil
}

func (apiImpl *ProjectSpritApiImpl) RemoveSummaryItem(ctx context.Context, req *project_sprit_api.RemoveSummaryItemRequest) (*project_sprit_api.RemoveSummaryItemResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	projectItem, _, roleItem, err := utils.GetProjectMemberRole(ctx, req.ProjectId, sessionItem.UserId)
	if err == utils.ErrNoProject {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_NO_PROJECT,
			ErrMsg: "项目不存在",
		}, nil
	} else if err == utils.ErrNoRole {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	} else if err != nil {
		return nil, err
	}
	if projectItem.Closed {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	exist, err := issue_dao.SpritDao.Exist(ctx, req.ProjectId, req.SpritId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}

	entryItem, err := project_entry_dao.EntryDao.Get(ctx, req.SpritId, req.ProjectId)
	if err != nil {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}
	if entryItem.EntryType != uint32(project_entry_api.ENTRY_TYPE_ENTRY_TYPE_SPRIT) {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}

	hasUpdatePerm := api_common.CheckEntryCanUpdate(sessionItem, projectItem, roleItem, entryItem)

	if !hasUpdatePerm {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	summaryItem, err := issue_dao.SummaryItemDao.Get(ctx, req.SpritId, req.SummaryItemId)
	if err != nil {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_NO_SUMMARY_ITEM,
			ErrMsg: "总结条目不存在",
		}, nil
	}

	if !(summaryItem.CreateUserId == sessionItem.UserId || sessionItem.UserId == projectItem.OwnerUserId || roleItem.BasicInfo.Admin) {
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code:   project_sprit_api.RemoveSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = issue_dao.SummaryItemDao.Remove(sessCtx, req.SpritId, req.SummaryItemId)
		if err != nil {
			return nil, err
		}
		return &project_sprit_api.RemoveSummaryItemResponse{
			Code: project_sprit_api.RemoveSummaryItemResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*project_sprit_api.RemoveSummaryItemResponse), nil
}

func (apiImpl *ProjectSpritApiImpl) GroupSummaryItem(ctx context.Context, req *project_sprit_api.GroupSummaryItemRequest) (*project_sprit_api.GroupSummaryItemResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	projectItem, _, roleItem, err := utils.GetProjectMemberRole(ctx, req.ProjectId, sessionItem.UserId)
	if err == utils.ErrNoProject {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_PROJECT,
			ErrMsg: "项目不存在",
		}, nil
	} else if err == utils.ErrNoRole {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	} else if err != nil {
		return nil, err
	}
	if projectItem.Closed {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	entryItem, err := project_entry_dao.EntryDao.Get(ctx, req.SpritId, req.ProjectId)
	if err != nil {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}
	if entryItem.EntryType != uint32(project_entry_api.ENTRY_TYPE_ENTRY_TYPE_SPRIT) {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}

	hasUpdatePerm := api_common.CheckEntryCanUpdate(sessionItem, projectItem, roleItem, entryItem)

	if !hasUpdatePerm {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	spritItem, err := issue_dao.SpritDao.Get(ctx, req.ProjectId, req.SpritId)
	if err != nil {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_SPRIT,
			ErrMsg: "迭代不存在",
		}, nil
	}
	if spritItem.SummaryState != uint32(project_sprit_api.SUMMARY_STATE_SUMMARY_SHOW) {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "不是总结展示阶段",
		}, nil
	}
	count, err := issue_dao.SummaryItemDao.CountById(ctx, req.SummaryItemIdList, req.SpritId)
	if err != nil {
		return nil, err
	}
	if int(count) != len(req.SummaryItemIdList) {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_SUMMARY_ITEM,
			ErrMsg: "总结条目不存在",
		}, nil
	}
	if !(projectItem.OwnerUserId == sessionItem.UserId || roleItem.BasicInfo.Admin) {
		return &project_sprit_api.GroupSummaryItemResponse{
			Code:   project_sprit_api.GroupSummaryItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	groupId := uuid.NewString()
	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = issue_dao.SummaryItemDao.UpdateGroup(sessCtx, req.SummaryItemIdList, req.SpritId, groupId)
		if err != nil {
			return nil, err
		}
		return &project_sprit_api.GroupSummaryItemResponse{
			Code: project_sprit_api.GroupSummaryItemResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*project_sprit_api.GroupSummaryItemResponse), nil
}
